package com.cskaoyan.market.service.impl;

import com.cskaoyan.market.db.domain.MarketStorage;
import com.cskaoyan.market.db.domain.MarketStorageExample;
import com.cskaoyan.market.db.mapper.MarketStorageMapper;
import com.cskaoyan.market.service.AdminStorageService;
import com.cskaoyan.market.util.JacksonUtil;
import com.cskaoyan.market.util.MybatisUtil;
import com.cskaoyan.market.util.ResponseUtil;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.github.pagehelper.PageHelper;
import org.apache.commons.lang3.StringUtils;
import org.apache.ibatis.session.SqlSession;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 刘国兴
 * @description 存储对象管理
 */
public class AdminStorageServiceImpl implements AdminStorageService {

    @Override
    public List<MarketStorage> list(Map<String, String[]> parameterMap) {

        SqlSession session = MybatisUtil.getSession();
        MarketStorageMapper mapper = session.getMapper(MarketStorageMapper.class);

        MarketStorageExample example = new MarketStorageExample();
        MarketStorageExample.Criteria criteria = example.createCriteria();

        //下面这步是把map中一个一个存着的字符连一起
        Map<String, String> map = new HashMap<>();
        parameterMap.forEach((key, value) -> map.put(key, StringUtils.join(value)));

        if (StringUtils.isNotEmpty(map.get("key"))) {
            criteria.andKeyEqualTo(map.get("key"));
        }
        if (StringUtils.isNotEmpty(map.get("name"))) {
            criteria.andNameLike("%"+map.get("name")+"%");
        }

        example.setOrderByClause(map.get("sort")+" "+map.get("order"));

        PageHelper.startPage(Integer.parseInt(map.get("page")), Integer.parseInt(map.get("limit")));

        List<MarketStorage> storageList = mapper.selectByExample(example);
        session.commit();
        session.close();
        return storageList;
    }

    @Override
    public String update(String reqJson) throws JsonProcessingException {
        SqlSession session = MybatisUtil.getSession();
        MarketStorageMapper mapper = session.getMapper(MarketStorageMapper.class);

        ObjectMapper objectMapper = JacksonUtil.getObjectMapper();
        MarketStorage marketStorage = objectMapper.readValue(reqJson, MarketStorage.class);

        marketStorage.setUpdateTime(LocalDateTime.now());

        //验证一下参数，没问题就执行
        if (marketStorage.getName() == null) {
            Object o = ResponseUtil.fail(401, "参数错误");
            session.close();
            return JacksonUtil.writeValueAsString(o);
        } else {
            mapper.updateByPrimaryKeySelective(marketStorage);
            session.commit();
            session.close();
            Object o = ResponseUtil.ok(marketStorage);
            return JacksonUtil.writeValueAsString(o);
        }
    }

    @Override
    public String delete(String reqJson) throws JsonProcessingException {
        SqlSession session = MybatisUtil.getSession();
        MarketStorageMapper mapper = session.getMapper(MarketStorageMapper.class);

        Integer id = JacksonUtil.parseInteger(reqJson, "id");
        MarketStorage marketStorage = mapper.selectByPrimaryKey(id);

        if (marketStorage != null) {
            mapper.deleteByPrimaryKey(id);
            session.commit();
            session.close();
            Object o = ResponseUtil.ok(marketStorage);
            return JacksonUtil.writeValueAsString(o);
        } else {
            session.commit();
            session.close();
            Object o = ResponseUtil.fail(401, "参数错误");
            return JacksonUtil.writeValueAsString(o);
        }
    }

    /**
     * @author 王鸣纯
     * @description 上传文件
     */
    @Override
    public void insert(MarketStorage marketStorage) {
        SqlSession session = MybatisUtil.getSession();
        MarketStorageMapper mapper = session.getMapper(MarketStorageMapper.class);
        mapper.insert(marketStorage);
        session.commit();
        session.close();
    }
}
